package com.dzm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzm.service.IEmpService;
import com.dzm.utils.PageResult;
import com.dzm.utils.R;
import com.dzm.vo.EmpVo;
import com.dzm.vo.SearchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Dzm
 * @since 2024-08-17
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "员工信息")
public class EmpController {

    @Autowired
    private IEmpService empService;

    @ApiOperation("员工条件分页查询列表")
    @PostMapping("/search/{pageNum}/{pageSize}")
    public R searchEmp(@PathVariable("pageNum")Integer pageNum,
                       @PathVariable("pageSize")Integer pageSize,
                       @RequestBody SearchVo searchVo) {
//        List<EmpVo> list = empService.searchEmp(pageNum, pageSize, searchVo);
//        Integer total = empService.empCount(searchVo);
//        PageResult<EmpVo> pageModel = new PageResult<EmpVo>(Long.valueOf(total + ""), list);
        IPage<EmpVo> pageModel = empService.searchByWrapper(pageNum, pageSize, searchVo);

        return R.ok().data("pageModel", pageModel);
    }

    @ApiOperation("根据员工id查询员工信息")
    @GetMapping("/getById/{eid}")
    public R findById(@PathVariable("eid") String eid) {
        EmpVo emp = empService.findById(eid);

        return R.ok().data("emp", emp);
    }

    @ApiOperation("修改员工信息")
    @PutMapping("/update")
    public R updateEmp(@RequestBody EmpVo empVo) {
        empService.updateEmp(empVo);
        return R.ok().message("修改成功！");
    }

    @ApiOperation("添加员工信息")
    @PostMapping("/add")
    public R addEmp(@RequestBody EmpVo empVo) {
        empService.addEmp(empVo);
        return R.ok().message("添加成功！");
    }

    @ApiOperation("删除员工信息")
    @DeleteMapping("/remove")
    public R  removeEmp(@RequestBody List<String> eids) {
//        List<String> ids = Arrays.asList(eids);
        empService.removeEmp(eids);

        return R.ok().message("删除成功！");
    }

}
